/*
 * @Author: CodeHare
 * @Date: 2023-11-06 17:12:39
 * @LastEditors: yangni 920556271@qq.com
 * @LastEditTime: 2024-04-25 15:58:08
 * @Description: 系统设置
 */
import 'package:get/get.dart';
import 'package:native_dart_package/native_dart_package.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:throttling/throttling.dart';
import 'package:yt_devops/components/log_utils.dart';
import 'package:yt_devops/pages/mine/system_set/system_set_model.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/routers/router_path.dart';
import 'package:yt_devops/services/auth/auth_services.dart';
import 'package:yt_devops/user_entity_manager.dart';
import 'package:yt_devops/utils/umeng/pusher.dart';

class SystemSetController extends GetxController {
  final SystemSetState state = SystemSetState();
  RefreshController refreshcontroller = RefreshController();
  final _authServices = Get.put(AuthServices());
  final throttleFunc = Throttling(duration: const Duration(seconds: 1));
  String languageString = '中文(简体)';

  Future<void> logout() async {
    /// 获取deviceToken
    var deviceToken = await UmengPusher.getDeviceToken();
    var params = {'deviceToken': deviceToken};

    Log.d('params=$params');
    showLoading(toast: ResKey.loading.tr);

    _authServices.logout(params).then((value) async {
      dismissLoading();
      UserEntityManager().cleanAuth();
      Log.d('登出接口请求得到的值====$value');

      /// 删除所有友盟tags
      await UmengPusher.removeTags();
      Get.offAllNamed(RouterPath.login);
      // update();
    }).catchError((error) {
      Log.d('登出接口请求报错信息为====$error');

      dismissLoading();
      showError(error?.message ?? '');
    });
  }

  void onClick(int index) {
    switch (index) {
      case 0:
        gotoLanguage();
        break;
      case 1:
        goAccountSecurity();
        break;
      default:
    }
  }

  void goMultilingual() {
    Get.toNamed(RouterPath.multilingual);
  }

  void goAccountSecurity() {
    Get.toNamed(RouterPath.security);
  }

  void goAccountLogOff() {
    Get.toNamed(RouterPath.accountLogOff);
  }

  void gotoLanguage() {
    Get.toNamed(RouterPath.language);
  }

  @override
  void onInit() {}

  @override
  void onClose() {
    throttleFunc.close();
    super.onClose();
  }
}
